package com.woniu.microcommunityserver.activity.mapper;

import com.woniu.microcommunityserver.activity.entity.ActivityManagementInfoEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface ActivityManagementInfoMapper {
    /**
     * 查询所有活动的所有信息
     * @return
     */
//    @Select("select id id, activity_title activityTitle,activity_cover_storage_address activityCoverStorageAddress, " +
//            "village_name villageName,activity_address activityAddress, activity_description activityDescription," +
//            "activity_start_time activityStartTime,activity_end_time activityEndTime," +
//            "activity_registration_start_time activityRegistrationStartTime," +
//            "activity_registration_end_time activityRegistrationEndTime," +
//            "activity_state activityState from activity_management_info")
    @Select("select id,activity_title,activity_cover_storage_address," +
            "village_name,activity_address,activity_description," +
            "activity_start_time,activity_end_time," +
            "activity_registration_start_time," +
            "activity_registration_end_time," +
            "activity_state from activity_management_info")
    @ResultMap("activityManagementInfoMap")
    List<ActivityManagementInfoEntity> findAllActivity();

    @Select({"<script> " +
            "select id,activity_title,activity_cover_storage_address," +
            "village_name,activity_address,activity_description," +
            "activity_start_time,activity_end_time," +
            "activity_registration_start_time," +
            "activity_registration_end_time," +
            "activity_state from activity_management_info " +
            "<where>" +
            "<if test='activityTitle!= null'>" +
            "and activity_title like concat('%', #{activityTitle}, '%')"+
            "</if>"+
            "<if test='villageName!= null'>" +
            "and village_name = #{villageName}"+
            "</if>"+
            "<if test='activityStartTime!= null'>" +
            "and activity_start_time &gt;= #{activityStartTime}"+
            "</if>"+
            "<if test='activityEndTime!= null'>" +
            "and activity_end_time &lt;= #{activityEndTime}"+
            "</if>"+
            "<if test='activityState!= null'>" +
            "and activity_state = #{activityState}"+
            "</if>"+
            "</where>"+
            "</script>"})
    @ResultMap("activityManagementInfoMap")
    List<ActivityManagementInfoEntity> findByAny(Map<String,Object> map);

    /**
     * 通过id删除指定活动
     * @param id
     * @return
     */
    @Delete("delete from activity_management_info where id=#{id}")
    boolean deleteActivity(Integer id);

    /**
     * 新增活动
     * @param activityManagementInfoEntity
     * @return
     */
    @Insert("insert into activity_management_info(activity_title,activity_cover_storage_address," +
            "village_name,activity_address,activity_description,activity_start_time," +
            "activity_end_time,activity_registration_start_time,activity_registration_end_time," +
            "activity_state) values(#{a.activityTitle},#{a.activityCoverStorageAddress},#{a.villageName}," +
            "#{a.activityAddress},#{a.activityDescription},#{a.activityStartTime},#{a.activityEndTime}," +
            "#{a.activityRegistrationStartTime},#{a.activityRegistrationEndTime},#{a.activityState})")
    boolean insertActivity(@Param("a") ActivityManagementInfoEntity activityManagementInfoEntity);

    /**
     * 修改活动内容
     * @param activityManagementInfoEntity
     * @return
     */
    @Update({"<script>",
            "update activity_management_info",
            "<set>",
            "<if test='activityTitle!= null'>",
            "activity_title = #{activityTitle} ,",
            "</if>",
            "<if test='activityCoverStorageAddress != null'>",
            "activity_cover_storage_address = #{activityCoverStorageAddress} ,",
            "</if>",
            "<if test='villageName != null'>",
            "village_name = #{villageName} ,",
            "</if>",
            "<if test='activityAddress != null'>",
            "activity_address = #{activityAddress} ,",
            "</if>",
            "<if test='activityDescription != null'>",
            "activity_description = #{activityDescription} ,",
            "</if>",
            "<if test='activityStartTime != null'>",
            "activity_start_time = #{activityStartTime} ,",
            "</if>",
            "<if test='activityEndTime != null'>",
            "activity_end_time = #{activityEndTime} ,",
            "</if>",
            "<if test='activityRegistrationStartTime != null'>",
            "activity_registration_start_time = #{activityRegistrationStartTime} ,",
            "</if>",
            "<if test='activityRegistrationEndTime != null'>",
            "activity_registration_end_time = #{activityRegistrationEndTime} ,",
            "</if>",
            "</set>",
            "where id = #{id}",
            "</script>"})
    boolean updateActivity(ActivityManagementInfoEntity activityManagementInfoEntity);
}
